<?php

namespace addons\mental\model;

use think\Model;
use think\Config;

class TestMain extends Model
{

    // 表名
    protected $name = 'mental_testmain';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'questionNum',
    ];


    public function getTypeList()
    {
        return ['skip' => __('Type skip'), 'mark' => __('Type mark')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * Undocumented function
     * 列表
     * @return Object
     */
    public function tableList($param,$uid = 0)
    {
        $limit = 20;
        if (isset($param['limit'])) {
            $limit = $param['limit'];
        }
        $whereArr = $this->tableWhere($param);
        return $this->field($whereArr['field'])->where($whereArr['where'])->order($whereArr['order'])->paginate($limit)->each(function($item, $key)use($uid){
            $isData = \think\Db::name('mental_collect')->where(['user_id'=>$uid,'main_id'=>$item->id])->find();
            $item->isCollect = $isData?true:false;
        });
    }


    /**
     * Undocumented function
     * 统一处理查询条件
     * @param [type] $param
     * @return void
     */
    public function tableWhere($param)
    {
        $where = [];
        $where['switch'] = 1;
        if (isset($param['type']) && !empty($param['type'])) {
            $where['type'] = $param['type'];
        }
        if (isset($param['category_id']) && !empty($param['category_id'])) {
            $where['category_id'] = $param['category_id'];
        }
        if (isset($param['title']) && !empty($param['title'])) {
            $where['title'] = ['like', '%' . $param['title'] . '%'];
        }
        if (isset($param['description']) && !empty($param['description'])) {
            $where['description'] = ['like', '%' . $param['description'] . '%'];
        }
        if (isset($param['order']) && in_array($param['order'], ['price desc', 'price asc', 'createtime desc', 'createtime asc', 'test_num desc', 'test_num asc'])) {
            $result['order'] = $param['order'];
        } else {
            $result['order'] = [];
        }
        
        if(isset($param['isRecommend']) && !empty($param['isRecommend'])){
            $asd = true;
            $mt =  \think\Db::name('mental_testmain')->where(['id'=>$param['isRecommend']])->find();
            if ($mt['recommendMainId']) {
                $idsCount = \think\Db::name('mental_testmain')->where(['id'=>['in',$mt['recommendMainId']]])->count();
                if ($idsCount>0) {
                    $where['id'] =[
                        ['IN',$mt['recommendMainId']],
                        ['NOT IN',$param['isRecommend']]
                    ];
                    
                    $asd = false;
                }
            }
            if ($asd) {
                // $where['recommendIs'] = 1;
                // $cid = $mt['category_id'];
                // $count = \think\Db::name('mental_testmain')->where(['category_id'=>$cid,'recommendIs'=>1])->count();
                // if ($count>1) {
                //     $where['category_id'] = $cid;
                // }

                $where['id'] = ['IN',0];
            }
        }
        
        // var_dump($asd,$where);die;
        $result['where'] = $where;
        $result['field'] = "id,type,title,description,price,image,test_num,pay_mode,ex1,ex2,ex3";
        return $result;
    }

    /**
     * Undocumented function
     * 详情
     * @return void
     */
    public function detail($id)
    {
        return $this->with(['Remark.User'])->field('id,category_id,type,title,description,content,price,image,test_num,pay_mode,ex1,ex2,ex3,question_time,rotationImg,team_price,isAge,isGender,btn_color')->where('id', '=', $id)->where('switch',1)->find();
    }

    public function getQuestionNumAttr($value,$data)
    {
        $count = \think\Db::name('mental_testson')->where('main_id',$data['id'])->count();
        return $count;
    }

    /**
     * Undocumented function
     * 测试详情
     * @return void
     */
    public function testDetail($id)
    {
        return $this->with(['Question.Question'])->field('id,type,title,image,test_num,pay_mode')->where('id', '=', $id)->where('switch',1)->find();
    }

    // public function getImageAttr($value)
    // {
    //     return cdnurl($value,true);
    // }

    public function Remark()
    {
        return $this->hasMany('Remark', 'main_id', 'id')->where('is_audit', '=', 1)->limit(10);
    }

    public function Question()
    {
        return $this->hasMany('Testson', 'main_id', 'id');
    }
}
